import logging
from logging import debug
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

import tables as tb
import re

def get_temp_data(data):
    """
    >>> temp_data = get_temp_data(load_data("../../bindata/test.h5"))
    """

    temp_names = []
    for name in data.dtype.names:
        match = re.search(r"^(.*temp.*)", name)
        if match:
            temp_names.append(name)
        else:
            pass
    debug(temp_names)
    temp_data = data[temp_names]
    debug(temp_data)
    return temp_data

def get_as_array(data, names):
    """ return the array
    """
    return

def load_data(h5_file, path="./", filt=["(activepoweravg > 200)", "(activepoweravg < 2200)"]):
    """
    >>> data = load_data("../../bindata/test.h5")
    """
    debug(h5_file)
    h5fil = tb.openFile((path + h5_file), mode='r')
    tab = h5fil.root.data
    data = _filter_h5data(tab, filt)
    #debug("Parameters in data")
    #debug(data.dtype.names)
    return data



def _filter_h5data(turbine_data, filt_strings, startnum=None, steplen=None, stopnum=-1):
    for i, filt in enumerate(filt_strings):
        if i == 0:
            filt_string = filt
        else:
            filt_string += " & " + filt
    filt_data = turbine_data.readWhere(filt_string, start=startnum, stop=stopnum, step=steplen)
    return filt_data


if __name__=='__main__':
    import read_from_h5
    import doctest
    doctest.testmod(read_from_h5, verbose=False)
